<!DOCTYPE html>
<html lang="en">
<head>
    <link rel="stylesheet" href="__STATIC__/plugins/layui/css/layui.css" media="all">
    <meta charset="UTF-8">
    <title>选择</title>
</head>
<body>
<form class="layui-form" action="">
    <div class="layui-form-item">
        <label class="layui-form-label">选择</label>
        <div class="layui-input-block">
            <select name="parent" lay-filter="parent" lay-verify="required">
                <option value="">---选择---</option>
                {foreach $naire.category as $c}
                    <option value="{$c.name}">{$c.name}</option>
                {/foreach}
            </select>
        </div>
    </div>
    {foreach $naire.category as $c}
    <div class="layui-form-item children" id="{$c.name}" style="display: none">
        <label class="layui-form-label">选择</label>
        <div class="layui-input-block">
            <select>
                <option value="">---选择---</option>
                {foreach $c.children as $s}
                <option value="{$s}">{$s}</option>
                {/foreach}
            </select>
        </div>
    </div>
    {/foreach}
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="demo">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
</body>
<script src="__STATIC__/plugins/layui/layui.js"></script>
<script>
    layui.use(['form','layer'], function () {
      var form=layui.form;
      var layer=layui.layer;
      window.$=layui.$;
      form.on('select(parent)',function(data){
          var value=data.value;
          $('#'+value).siblings('.children').hide()
              .find('select').attr('readonly','readonly').removeAttr('name').removeAttr('lay-verify')
              .end().end().show().find('select').removeAttr('readonly').attr('name','children').attr('lay-verify',"required")
      });
        form.on('submit(demo)', function(){
            $.post("{:url('answer/choose')}",$('form').serialize(),function(response){
                layer.msg(response.msg);
                if(response.code==0){
                    var index=parent.layer.getFrameIndex(window.name);
                    setTimeout(function(){
                        parent.layer.close(index);
                    },1000);
                }
            });
            return false;
        });
    });
</script>
</html>